package com.sx.wechat.controller;

import com.alibaba.fastjson.JSON;
import com.sx.core.model.Information;
import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.utils.JwtUtils;
import com.sx.core.utils.Md5;
import com.sx.core.vo.JsonReaderResponse;
import com.sx.core.vo.Params;
import com.sx.wechat.security.Principal;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lihao on 16/12/21.
 */
@RestController
@RequestMapping("common")
public class CommonController extends BaseController {
    @Autowired
    SessionTemplate sessionTemplate;
    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 微信充值异步通知
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "login")
    public JsonReaderResponse openid(HttpServletRequest request, HttpServletResponse response) {
        String username=obtain(request,"username");
        String password=obtain(request,"password");
        String openid=obtain(request,"openid");
        Information information=sessionTemplate.get(Information.class,"selectBySno",new Params().p("sno",username));
        if(information==null){
            return new JsonReaderResponse("用户不存在",101);
        }
//        if(!StringUtils.equals(information.getType(),"2")){
//            return new JsonReaderResponse("无法登陆",101);
//        }
        if(!StringUtils.equals(ObjectUtils.toString(information.getPassword()), Md5.md5(password))){
            return new JsonReaderResponse("用户名密码错误",101);
        }
        if(StringUtils.isNotBlank(openid)){
            String sql="UPDATE t_information SET openid=? WHERE id=?";
            jdbcTemplate.update(sql,openid,information.getId());
        }


        Principal principal=new Principal();
        principal.setBindno(information.getSno());
        principal.setId(information.getId());
        principal.setType(information.getType());
        String sub_ject= JSON.toJSONString(principal);
        String token= JwtUtils.createJWT(information.getId(),sub_ject);
        information.setAccess_token(token);
        return new JsonReaderResponse().addExtra(information);

    }
    /**
     * 微信充值异步通知
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "pc_login")
    public JsonReaderResponse stu_login(HttpServletRequest request, HttpServletResponse response) {
        String username=obtain(request,"username");
        String password=obtain(request,"password");
        String openid=obtain(request,"openid");
        Information information=sessionTemplate.get(Information.class,"selectBySno",new Params().p("sno",username));
        if(information==null){
            return new JsonReaderResponse("用户不存在",101);
        }
        if(!StringUtils.equals(information.getType(),"2")){
            return new JsonReaderResponse("无法登陆",101);
        }
        if(!StringUtils.equals(ObjectUtils.toString(information.getPassword()), Md5.md5(password))){
            return new JsonReaderResponse("用户名密码错误",101);
        }
        if(StringUtils.isNotBlank(openid)){
            String sql="UPDATE t_information SET openid=? WHERE id=?";
            jdbcTemplate.update(sql,openid,information.getId());
        }


        Principal principal=new Principal();
        principal.setBindno(information.getSno());
        principal.setId(information.getId());
        principal.setType(information.getType());
        String sub_ject= JSON.toJSONString(principal);
        String token= JwtUtils.createJWT(information.getId(),sub_ject);
        information.setAccess_token(token);
        return new JsonReaderResponse().addExtra(information);

    }


}
